#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui
from Ui_dialog_add_new_attribute import *
from dialog_new_attribute_type import dialog_new_attribute_type

class dialog_add_new_attribute(QtGui.QDialog, Ui_Dialog_add_new_attribute):
    
    
    def __init__(self, window_parent, libmyCF, attribute_type_id, value):
        super(dialog_add_new_attribute,self).__init__(window_parent)
        
        #options
        # "add mode" : you can select a type attribute, set its value, and add it to the AE
        # "edit mode" : you can set the value of an existing attribute. You need to specify attribute_type_id and its value when calling the function
        
        self.setupUi(self)
        self.parent=window_parent
        self.libmyCF = libmyCF

        self.previous_index = 0
        self.take_account_of_index_change = True
        self.add_mode = True
        self.edit_mode = False
        
        if attribute_type_id != None and value != None :
            self.add_mode = False
            self.edit_mode = True
        
        
        self.connect(self.buttonBox, QtCore.SIGNAL("accepted()"),self, QtCore.SLOT("accept()"))
        self.connect(self.buttonBox, QtCore.SIGNAL("rejected()"),self, QtCore.SLOT("reject()"))
    
        # List of the attribute types (only see the name in the comboBox).
        self.comboBox_name.clear() 
        self.liste_attribute_types = self.libmyCF.ontology.all_attributes_type
        
        index = 0        
        for attribute_type_key in self.libmyCF.ontology.all_attributes_type:            
            a = self.libmyCF.ontology.all_attributes_type[attribute_type_key]
            self.comboBox_name.insertItem (index, QtCore.QString.fromUtf8(a.name), QtCore.QVariant(a.attribute_type_id))           
            #print a.attribute_type_id
            index += 1
        self.comboBox_name.insertItem (index, QtGui.QIcon("../ui/icons/db_add.png"),QtCore.QString.fromUtf8("Add a new attribute type..."),QtCore.QVariant(0))
        self.comboBox_name.insertSeparator(index)
        
        if self.edit_mode:
            self.setWindowTitle(QtGui.QApplication.translate("Dialog_add_new_attribute", "mcf: edit attribute value", None, QtGui.QApplication.UnicodeUTF8))
            self.comboBox_name.setEnabled(False)
            self.label_name.setEnabled(False)
    
        #Set the type and the value in the lineEdit fields
        selected_attribute_type_id = self.comboBox_name.itemData(self.comboBox_name.currentIndex()).toInt()[0]
        self.selected_attribute_type = self.libmyCF.ontology.all_attributes_type[selected_attribute_type_id]
        self.lineEdit_type.setText(QtCore.QString.fromUtf8(str(self.selected_attribute_type.type)))
        self.textEdit_definition.setText(QtCore.QString.fromUtf8(str(self.selected_attribute_type.definition)))
        self.lineEdit_example.setText(QtCore.QString.fromUtf8(str(self.selected_attribute_type.example)))
        self.lineEdit_value.setText("")
        if self.edit_mode :
            self.lineEdit_value.setText(value)
            
    def on_comboBox_name_currentIndexChanged(self,index):
        if self.take_account_of_index_change == True :
            if self.comboBox_name.currentText() == "Add a new attribute type..." :
                new_att_type_dia = dialog_new_attribute_type(self,self.libmyCF.ontology)
                if not new_att_type_dia.exec_():
                    self.comboBox_name.setCurrentIndex(self.previous_index)
                else :
                    #retrieve the name, type and value of the new type attribute from the dialog
                    name = new_att_type_dia.name
                    type = new_att_type_dia.type
                    definition = new_att_type_dia.definition
                    example = new_att_type_dia.example 

                    #Create he attribute type in the database nd the local ontology and retrieve its ID.
                    newAtt = self.libmyCF.ontology.add_attribute_type( name,type,definition,example)
                    att_type_id = newAtt.getId()

                    #add the new type attribute in the combobox
                    index = self.comboBox_name.currentIndex()
                    # The insertion of an item in the combobox triggers the signal of changed index. The current function is called again
                    # and since the index is the index of "add new attribute type...", it opens the dialog again and again
                    self.take_account_of_index_change = False
                    self.comboBox_name.insertItem( int( index - 1), QtCore.QString.fromUtf8( name), QtCore.QVariant(att_type_id))
                    self.take_account_of_index_change = True
                    self.comboBox_name.setCurrentIndex( int( index - 1))
                    #set the lineEdit fields with the type of the new type attribute and with an empty value
                    self.lineEdit_type.setText( str( self.selected_attribute_type.type))
                    self.textEdit_definition.setText( QtCore.QString.fromUtf8( str( self.selected_attribute_type.definition)))
                    self.lineEdit_example.setText( QtCore.QString.fromUtf8( str( self.selected_attribute_type.example)))
                    self.lineEdit_value.setText("")

            else :
                self.selected_attribute_type_id = self.comboBox_name.itemData( self.comboBox_name.currentIndex() ).toInt()[0]
                self.selected_attribute_type = self.libmyCF.ontology.all_attributes_type[ self.selected_attribute_type_id]
                self.lineEdit_type.setText( self.selected_attribute_type.type)
                self.textEdit_definition.setText( QtCore.QString.fromUtf8( str( self.selected_attribute_type.definition )))
                self.lineEdit_example.setText( QtCore.QString.fromUtf8( str( self.selected_attribute_type.example )))
                self.lineEdit_value.setText("")
                self.previous_index = self.comboBox_name.currentIndex()

    def accept(self):

        self.new_attribute_type = (self.comboBox_name.currentText() == "Add a new attribute type...")

        if not self.new_attribute_type :

            self.selected_attribute_type_id = self.comboBox_name.itemData( self.comboBox_name.currentIndex()).toInt()[0]
            self.selected_AE_id = self.parent.AE.AE_id
            self.selected_value = unicode( self.lineEdit_value.text()).encode('utf-8')

            if self.selected_value == "":
                QtGui.QMessageBox.warning(self,"myCF information","You must enter a value for the selected attribute")
                return

            QtGui.QDialog.accept(self)


        return